package com.biz.primus.base.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * @author yanweijin
 * @date 2016/12/19
 */
@ApiModel("基础分页对象")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PageableRequestVo implements Serializable {
    private static final long serialVersionUID = -7408530622119545340L;

    @ApiModelProperty("页码0开始")
    private int page = 0;

    @ApiModelProperty("分页大小默认20")
    private int size = 20;

    public PageRequest toPageRequest(){
        return new PageRequest(page,size);
    }
    public PageRequest toPageRequest(Sort sort){
        return new PageRequest(page,size, sort);
    }

    public PageRequest buildPageRequest(Sort sort) {
        return new PageRequest(Math.max(0, page - 1), size, sort);
    }
}
